package com.ganzalang.gmall.designpatterns.template;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 使用模板方法
 * @author: ganzalang
 */
@Slf4j
public class TemplatePatternDemo {

    /**
     * 模板方法其实就是抽取公共逻辑到一个抽象类中
     * @param args
     */
    public static void main(String[] args) {
        DiscountCalculator1 discountCalculator1 = new DiscountCalculator1();
        DiscountCalculator2 discountCalculator2 = new DiscountCalculator2();
        DiscountCalculator3 discountCalculator3 = new DiscountCalculator3();
        discountCalculator1.doCalculate();
        discountCalculator2.doCalculate();
        discountCalculator3.doCalculate();
    }

    public interface DiscountCalculator {
        void doCalculate();
    }

    public static abstract class AbstractDiscountCalculator implements DiscountCalculator{

        @Override
        public void doCalculate() {
            commonDiscountCalcul();
            specificCalculate();
        }

        private void commonDiscountCalcul() {
            log.info("公共计算");
        }

        public abstract void specificCalculate();
    }

    public static class DiscountCalculator1 extends AbstractDiscountCalculator {

        @Override
        public void specificCalculate() {
            log.info("折扣1计算逻辑");
        }
    }

     public static class DiscountCalculator2 extends AbstractDiscountCalculator {

        @Override
        public void specificCalculate() {
            log.info("折扣2计算逻辑");
        }
    }

     public static class DiscountCalculator3 extends AbstractDiscountCalculator {

        @Override
        public void specificCalculate() {
            log.info("折扣3计算逻辑");
        }
    }


}
